#
#  gStorageBrowserController.py
#  gStorageBrowser
#
#  Created by Wang Shao-chuan on 2011/1/1.
#  Copyright (c) 2011 National Taiwan University. All rights reserved.
#

from Foundation import *
import objc
from gStorageModel import gStroageRootNode

class gStorageBrowserController(NSObject):
  browser = objc.IBOutlet()
  
  def _parentNodeOfColumn(self, column):
    result = self.rootNode
    for i in xrange(column):
      selectedRowInColumn = self.browser.selectedRowInColumn_(i)
      result = result.atRow(selectedRowInColumn)
    return result
    
  def awakeFromNib(self):
    NSLog("Awake from Nib.")
    self.rootNode = gStroageRootNode()
    
  def browser_numberOfRowsInColumn_(self, sender, column):
    NSLog("browser_numberOfRowsInColumn_: %d", self._parentNodeOfColumn(column).childrenCount )
    return self._parentNodeOfColumn(column).childrenCount
    
  def browser_willDisplayCell_atRow_column_(self, sender, cell, row, column):
    NSLog("browser_willDisplayCell_atRow_column_")
    parentNode = self._parentNodeOfColumn(column)
    childNode = parentNode.atRow(row)
    cell.setTitle_(childNode.title)
    cell.setLeaf_(childNode.isLeaf)